package com.briup.day12;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @author 谭梦寻
 * @version 1.1
 */
@SuppressWarnings("all")
public class CollectionIterator {

    public static void main(String[] args) {
        Collection col = new ArrayList();
        col.add(new Book("三国演义", "罗贯中", 67.2));
        col.add(new Book("红楼梦", "曹雪芹", 59.9));
        col.add(new Book("西游记", "施耐庵", 65.3));

//        1 先得到 col 对应的 迭代器
        Iterator iterator = col.iterator();

//        2 使用while循环遍历
        while (iterator.hasNext()) {//判断是否还有元素
            Object obj = iterator.next();
            System.out.println(obj);
        }
//        3 当退出while循环后 这时iterator迭代器指向最后的元素
//        iterator.next();//NoSuchElementException
//        4 如果希望再次便利 需要重置我们的迭代器
        iterator = col.iterator();
        System.out.println("=====第二次遍历======");
//        快捷方式 itit
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println("obj=" + obj);
        }

//        使用增强for循环 也可以用于数组 底层仍然是迭代器
//        快捷方式 I

        for (Object book : col) {
            System.out.println("book=" + book);
        }
        int[] nums = {1, 2, 3, 4, 5};
        for (int i : nums) {
            System.out.println("i=" + i);
        }


//
        List list = new ArrayList();
        list.add(new Book("三国演义", "罗贯中", 67.2));
        list.add(new Book("红楼梦", "曹雪芹", 59.9));
        list.add(new Book("西游记", "施耐庵", 65.3));
        list.add(new Book("java", "xxx", 39.5));
        list.add(new Book("JavaScript", "xxx", 42.5));
        System.out.println("list1=" + list);

        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.size() - 1 - i; j++) {
//                取出书再比较后放回去
                Book book1 = (Book) list.get(j);//list.get()返回的是Object类型
                Book book2 = (Book) list.get(j + 1);//list.get()返回的是Object类型
//                System.out.println("list="+list);
                if (book1.getPrice() > book2.getPrice()) {
                    list.set(j, book2);
                    list.set(j + 1, book1);

                }
            }
        }
        System.out.println("list3=" + list);

    }

}

class Book implements Comparable{
    private String name;
    private String author;
    private double price;

    public Book() {
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public int compareTo(Object o) {
//        通过比较方法 如果返回值为0 表示相同 树中不存储相同的元素，1 允许
        return 0;
    }
}
